package com.hi.hiparty.dao;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.orm.ibatis.SqlMapClientTemplate;
import org.springframework.stereotype.Repository;

import com.hi.hiparty.entity.Post;
import com.hi.hiparty.jdbc.SqlMapClientTemplateCache;
import com.hi.hiparty.util.IDGenerator;

@Repository
public class PostDao {

	protected SqlMapClientTemplate getSqlMapClientTemplate() throws Exception {
		return SqlMapClientTemplateCache.getSqlMapClientTemplate("hiparty");
	}

	public Post insert(Post post) throws Exception {
		if (null == post.getPartyid()) {
			return null;
		}
		post.setPostid(IDGenerator.generatePostid(post.getPartyid()));
		this.getSqlMapClientTemplate().insert("Post.insert", post);
		return post;
	}

	@SuppressWarnings("unchecked")
	public List<Post> list(String partyid, int start, int size) throws Exception {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("partyid", partyid);
		params.put("start", start);
		params.put("size", size);
		return this.getSqlMapClientTemplate().queryForList("Post.listByPartyid", params);
	}

}
